{% load humanize i18n icons permissions translations %}

{% if objects or list_categories %}
  <table {% if objects.paginator.num_pages > 1 %} class="table progress-table autocolspan table-listing" {% else %} class="sort table progress-table autocolspan table-listing" {% endif %}>
    {% if not hide_details and not hide_header %}
      <thead class="sticky-header">
        <tr>
          <th class="sort-skip">
            {% if add_link == "translation" %}
              {% if user_can_add_translation %}
                <a class="btn btn-link btn-sm"
                   id="list-add-button"
                   href="{% url "new-language" path=object.get_url_path %}"
                   title="{% translate "Start new translation" %}">{% icon "plus.svg" %}</a>
              {% endif %}
            {% elif add_link == "component" %}
              {% if user_can_edit_project %}
                <a class="btn btn-link btn-sm"
                   id="list-add-button"
                   href="{% url 'create-component' %}?project={{ object.pk }}"
                   title="{% translate "Add new translation component" %}">{% icon "plus.svg" %}</a>
              {% endif %}
            {% elif add_link == "component-category" %}
              {% if user_can_edit_project %}
                <a class="btn btn-link btn-sm"
                   id="list-add-button"
                   href="{% url 'create-component' %}?project={{ object.project.pk }}&amp;category={{ object.pk }}"
                   title="{% translate "Add new translation component" %}">{% icon "plus.svg" %}</a>
              {% endif %}
            {% elif add_link == "language" %}
              {% if user_can_add_language %}
                <a class="btn btn-link btn-sm"
                   id="list-add-button"
                   href="{% url 'create-language' %}"
                   title="{% translate "Add language" %}">{% icon "plus.svg" %}</a>
              {% endif %}
            {% elif add_link == "project" %}
              {% if user.is_authenticated %}
                <a class="btn btn-link btn-sm"
                   id="list-add-button"
                   href="{% url 'create-project' %}"
                   title="{% translate "Add project" %}">{% icon "plus.svg" %}</a>
              {% endif %}
            {% endif %}
          </th>
          <th title="{% translate "Sort this column" %}" class="sort-cell">
            {% if objects.paginator.num_pages > 1 %}
              <a href="?page={{ objects.number }}&amp;sort_by={% if objects.paginator.sort_by == "name" %}-{% endif %}name">
              {% endif %}
              {{ label|default:_("Project") }}
              <span class="sort-icon {% if objects.paginator.sort_by == "name" %}sort-down{% elif objects.paginator.sort_by == "-name" %}sort-up{% endif %}" />
              {% if objects.paginator.num_pages > 1 %}</a>{% endif %}
          </th>
          {% if project and project.enable_review %}
            <th title="{% translate "Sort this column" %}"
                class="number zero-width-540 sort-cell">
              {% if objects.paginator.num_pages > 1 %}
                <a href="?page={{ objects.number }}&amp;sort_by={% if objects.paginator.sort_by == "approved" %}-{% endif %}approved">
                {% endif %}
                <span class="sort-icon {% if objects.paginator.sort_by == "approved" %}sort-down{% elif objects.paginator.sort_by == "-approved" %}sort-up{% endif %}"> </span>
                {% translate "Approved" %}
                {% if objects.paginator.num_pages > 1 %}</a>{% endif %}
            </th>
          {% endif %}
          <th title="{% translate "Sort this column" %}" class="number sort-cell">
            {% if objects.paginator.num_pages > 1 %}
              <a href="?page={{ objects.number }}&amp;sort_by={% if objects.paginator.sort_by == "translated" %}-{% endif %}translated">
              {% endif %}
              <span class="sort-icon {% if objects.paginator.sort_by == "translated" %}sort-down{% elif objects.paginator.sort_by == "-translated" %}sort-up{% endif %}"> </span>
              {% translate "Translated" %}
              {% if objects.paginator.num_pages > 1 %}</a>{% endif %}
          </th>
          <th title="{% translate "Sort this column" %}"
              class="number zero-width-640 sort-cell">
            {% if objects.paginator.num_pages > 1 %}
              <a href="?page={{ objects.number }}&amp;sort_by={% if objects.paginator.sort_by == "untranslated" %}-{% endif %}untranslated">
              {% endif %}
              <span class="sort-icon {% if objects.paginator.sort_by == "untranslated" %}sort-down{% elif objects.paginator.sort_by == "-untranslated" %}sort-up{% endif %}"> </span>
              {% translate "Unfinished" %}
              {% if objects.paginator.num_pages > 1 %}</a>{% endif %}
          </th>
          <th title="{% translate "Sort this column" %}"
              class="number zero-width-720 sort-cell">
            {% if objects.paginator.num_pages > 1 %}
              <a href="?page={{ objects.number }}&amp;sort_by={% if objects.paginator.sort_by == "untranslated_words" %}-{% endif %}untranslated_words">
              {% endif %}
              <span class="sort-icon {% if objects.paginator.sort_by == "untranslated_words" %}sort-down{% elif objects.paginator.sort_by == "-untranslated_words" %}sort-up{% endif %}"> </span>
              {% translate "Unfinished words" %}
              {% if objects.paginator.num_pages > 1 %}</a>{% endif %}
          </th>
          <th title="{% translate "Sort this column" %}"
              class="number zero-width-1200 sort-cell">
            {% if objects.paginator.num_pages > 1 %}
              <a href="?page={{ objects.number }}&amp;sort_by={% if objects.paginator.sort_by == "untranslated_chars" %}-{% endif %}untranslated_chars">
              {% endif %}
              <span class="sort-icon {% if objects.paginator.sort_by == "untranslated_chars" %}sort-down{% elif objects.paginator.sort_by == "-untranslated_chars" %}sort-up{% endif %}"> </span>
              {% translate "Unfinished characters" %}
              {% if objects.paginator.num_pages > 1 %}</a>{% endif %}
          </th>
          <th title="{% translate "Sort this column" %}"
              class="number zero-width-1400 sort-cell">
            {% if objects.paginator.num_pages > 1 %}
              <a href="?page={{ objects.number }}&amp;sort_by={% if objects.paginator.sort_by == "nottranslated" %}-{% endif %}nottranslated">
              {% endif %}
              <span class="sort-icon {% if objects.paginator.sort_by == "nottranslated" %}sort-down{% elif objects.paginator.sort_by == "-nottranslated" %}sort-up{% endif %}"> </span>
              {% translate "Untranslated" %}
              {% if objects.paginator.num_pages > 1 %}</a>{% endif %}
          </th>
          <th title="{% translate "Sort this column" %}"
              class="number zero-width-768 sort-cell">
            {% if objects.paginator.num_pages > 1 %}
              <a href="?page={{ objects.number }}&amp;sort_by={% if objects.paginator.sort_by == "checks" %}-{% endif %}checks">
              {% endif %}
              <span class="sort-icon {% if objects.paginator.sort_by == "checks" %}sort-down{% elif objects.paginator.sort_by == "-checks" %}sort-up{% endif %}"> </span>
              {% translate "Checks" %}
              {% if objects.paginator.num_pages > 1 %}</a>{% endif %}
          </th>
          <th title="{% translate "Sort this column" %}"
              class="number zero-width-900 sort-cell">
            {% if objects.paginator.num_pages > 1 %}
              <a href="?page={{ objects.number }}&amp;sort_by={% if objects.paginator.sort_by == "suggestions" %}-{% endif %}suggestions">
              {% endif %}
              <span class="sort-icon {% if objects.paginator.sort_by == "suggestions" %}sort-down{% elif objects.paginator.sort_by == "-suggestions" %}sort-up{% endif %}"> </span>
              {% translate "Suggestions" %}
              {% if objects.paginator.num_pages > 1 %}</a>{% endif %}
          </th>
          <th title="{% translate "Sort this column" %}"
              class="number zero-width-1000 sort-cell">
            {% if objects.paginator.num_pages > 1 %}
              <a href="?page={{ objects.number }}&amp;sort_by={% if objects.paginator.sort_by == "comments" %}-{% endif %}comments">
              {% endif %}
              <span class="sort-icon {% if objects.paginator.sort_by == "comments" %}sort-down{% elif objects.paginator.sort_by == "-comments" %}sort-up{% endif %}"> </span>
              {% translate "Comments" %}
              {% if objects.paginator.num_pages > 1 %}</a>{% endif %}
          </th>
        </tr>
      </thead>
    {% endif %}


    <tbody>
      {% init_unique_row_id %}
      {% for category in list_categories %}
        {% get_unique_row_id category as row_id %}
        {% get_search_url category as search_url %}
        {% get_translate_url category as translate_url %}
        <tr id="{{ row_id }}">
          <td class="actions" rowspan="2">
            <a href="{{ category.get_absolute_url }}"
               class="btn btn-link green"
               title="{% translate "Browse" %}"
               alt="{% translate "Browse" %}">{% icon "folder-multiple-outline.svg" %}</a>
          </td>
          <th class="object-link">
            <a href="{{ category.get_absolute_url }}">{{ category.name }}</a>
          </th>
          {% if project and project.enable_review %}
            {% review_percent category.stats %}
          {% endif %}
          {% list_objects_percent percent=category.stats.translated_percent value=category.stats.translated search_url=search_url translate_url=translate_url query="q=state:>=translated" total=category.stats.all checks=category.stats.allchecks %}
          {% if not hide_details %}
            {% list_objects_number value=category.stats.todo search_url=search_url translate_url=translate_url query="q=state:<translated " css="zero-width-640" %} {% list_objects_number value=category.stats.todo_words search_url=search_url translate_url=translate_url query="q=state:<translated" css="zero-width-720" %} {% list_objects_number value=category.stats.todo_chars search_url=search_url translate_url=translate_url query="q=state:<translated" css="zero-width-1200" %} {% list_objects_number value=category.stats.nottranslated search_url=search_url translate_url=translate_url query="q=state:empty" css="zero-width-1400" %} {% list_objects_number value=category.stats.allchecks search_url=search_url translate_url=translate_url query="q=has:check" css="zero-width-768" %} {% list_objects_number value=category.stats.suggestions search_url=search_url translate_url=translate_url query="q=has:suggestion#suggestions" css="zero-width-900" %} {% list_objects_number value=category.stats.comments search_url=search_url translate_url=translate_url query="q=has:comment#comments" css="zero-width-1000" %}
          {% endif %}
        </tr>
        <tr data-parent="{{ row_id }}">
          <td colspan="7" class="progress-row autocolspan">{% translation_progress category.stats %}</td>
        </tr>
      {% endfor %}
      {% init_unique_row_id %}
      {% for object in objects %}
        {% get_translate_url object as translate_url %}
        {% get_browse_url object as browse_url %}
        {% get_search_url object as search_url %}
        {% get_unique_row_id object as row_id %}
        <tr id="{{ row_id }}">
          <td class="actions" rowspan="2">
            {% if object.is_ghost %}
              {% perm 'translation.add' object.base_obj as user_can_add_translation %}
              {% if user_can_add_translation %}
                <form action="{% url 'new-language' path=object.base_obj.get_url_path %}"
                      method="post">
                  {% csrf_token %}
                  <input type="hidden" name="lang" value="{{ object.language.code }}" />
                  <button type="submit"
                          alt="{% translate "Create translation" %}"
                          title="{% translate "Create translation" %}"
                          class="btn btn-link green">{% icon "plus.svg" %}</button>
                </form>
              {% else %}
                <span class="gray"
                      title="{% if user_can_add_translation.reason %}{{ user_can_add_translation.reason }}{% else %}{% translate "Cannot add translation right now" %}{% endif %}">{% icon "plus.svg" %}</span>
              {% endif %}

            {% else %}

              {% if translate_url %}
                <a href="{{ translate_url }}{% if object.stats.todo %}?q=state:&lt;translated{% endif %}"
                   class="btn btn-link green"
                   title="{% translate "Translate" %}"
                   alt="{% translate "Translate" %}">{% icon "pencil.svg" %}</a>
              {% else %}
                <a href="{{ browse_url }}"
                   class="btn btn-link green"
                   title="{% translate "Browse" %}"
                   alt="{% translate "Browse" %}">{% icon "folder-outline.svg" %}</a>
              {% endif %}

            {% endif %}
          </td>
          <th class="object-link">
            {% if name_source == "component_name" or name_source == "translation" %}
              {% get_glossary_badge object.component %}
            {% else %}
              {% get_glossary_badge object %}
            {% endif %}

            {% if object.is_ghost %}
              <span class="ghost">
                {% if name_source == "language" %}
                  {{ object.language }}
                {% elif name_source == "component_name" %}
                  {{ object.component.name }}
                {% elif name_source == "name" %}
                  {{ object.name }}
                {% elif name_source == "language" %}
                  {{ object.language }}
                {% elif name_source == "project" %}
                  {{ object.project }}
                {% else %}
                  {{ object }}
                {% endif %}
              </span>
            {% elif name_source == "translation" %}
              <a href="{{ object.component.project.get_absolute_url }}">{{ object.component.project.name }}</a>/<a href="{{ object.component.get_absolute_url }}">{{ object.component.name }}</a> — <a class="tail" href="{{ browse_url }}">{{ object.language }}</a>
            {% else %}
              <a href="{{ browse_url }}">
                {% spaceless %}

                  {% if name_source == "language" %}
                    {{ object.language }}
                  {% elif name_source == "component_name" %}
                    {{ object.component.name }}
                  {% elif name_source == "name" %}
                    {{ object.name }}
                  {% elif name_source == "language" %}
                    {{ object.language }}
                  {% elif name_source == "project" %}
                    {{ object.project }}
                  {% else %}
                    {{ object }}
                  {% endif %}

                {% endspaceless %}
              </a>
            {% endif %}
            {% indicate_alerts object %}
            {% if custom_workflows and object.pk in custom_workflows %}
              <span class="badge">{% translate "Customized workflow" %}</span>
            {% endif %}
          </th>
          {% if project and project.enable_review %}
            {% review_percent object.stats %}
          {% endif %}

          {% if is_glossary %}
            {% list_objects_number value=object.stats.translated search_url=search_url translate_url=translate_url query="q=state:>=translated" show_zero=True %}
          {% else %}
            {% list_objects_percent percent=object.stats.translated_percent value=object.stats.translated search_url=search_url translate_url=translate_url query="q=state:>=translated" total=object.stats.all checks=object.stats.allchecks %}
          {% endif %}
          {% if not hide_details %}
            {% list_objects_number value=object.stats.todo search_url=search_url translate_url=translate_url query="q=state:<translated" css="zero-width-640" %}
            {% list_objects_number value=object.stats.todo_words search_url=search_url translate_url=translate_url query="q=state:<translated" css="zero-width-720" %}
            {% list_objects_number value=object.stats.todo_chars search_url=search_url translate_url=translate_url query="q=state:<translated" css="zero-width-1200" %}
            {% list_objects_number value=object.stats.nottranslated search_url=search_url translate_url=translate_url query="q=state:empty" css="zero-width-1400" %}
            {% list_objects_number value=object.stats.allchecks search_url=search_url translate_url=translate_url query="q=has:check" css="zero-width-768" %}
            {% list_objects_number value=object.stats.suggestions search_url=search_url translate_url=translate_url query="q=has:suggestion#suggestions" css="zero-width-900" %}
            {% list_objects_number value=object.stats.comments search_url=search_url translate_url=translate_url query="q=has:comment#comments" css="zero-width-1000" %}
          {% endif %}
        </tr>
        <tr data-parent="{{ row_id }}">
          <td colspan="7" class="progress-row autocolspan">{% translation_progress object.stats %}</td>
        </tr>
      {% endfor %}
    </tbody>
  </table>
{% else %}
  <p>
    <em>
      {% if empty_message %}
        {{ empty_message }}
      {% else %}
        {% translate "Nothing to list here." %}
      {% endif %}
    </em>
  </p>

  <p>
    {% if add_link == "translation" %}
      {% if user_can_add_translation %}
        <a class="btn btn-primary"
           id="list-add-button"
           href="{% url "new-language" path=object.get_url_path %}">{% translate "Start new translation" %}</a>
      {% else %}
        <a class="btn btn-primary"
           id="list-add-button"
           disabled
           href="#"
           title="{% if user.is_authenticated %}{% translate "You don't have permission to start a new translation" %}{% else %}{% translate "Please sign in to start new translation" %}{% endif %}">{% translate "Start new translation" %}</a>
      {% endif %}
    {% elif add_link == "component" %}
      {% if user_can_edit_project %}
        {% if object.project %}
          <a class="btn btn-primary"
             id="list-add-button"
             href="{% url 'create-component' %}?project={{ object.project.pk }}&amp;category={{ object.pk }}">{% translate "Add new translation component" %}</a>
        {% else %}
          <a class="btn btn-primary"
             id="list-add-button"
             href="{% url 'create-component' %}?project={{ object.pk }}">{% translate "Add new translation component" %}</a>
        {% endif %}
      {% endif %}
    {% elif add_link == "language" %}
      {% if user_can_add_language %}
        <a class="btn btn-primary" id="list-add-button" href="{% url 'create-language' %}">{% translate "Add language" %}</a>
      {% endif %}
    {% endif %}
  </p>

{% endif %}
